package uit.comstore.controller;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import uit.comstore.model.blo.ImportBillBLO;
import uit.comstore.model.blo.ImportBillDetailBLO;
import uit.comstore.model.blo.ProductBLO;
import uit.comstore.model.blo.SupplierBLO;
import uit.comstore.model.entity.ImportBill;
import uit.comstore.model.entity.ImportBillDetail;
import uit.comstore.model.entity.Product;
import uit.comstore.model.entity.Supplier;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class ImportBillAction extends ActionSupport{
	private ImportBillBLO importBillBLO;
	private List<ImportBill> importBillList;
	private ImportBill importBill;
	private SupplierBLO supplierBLO;
	private List<Supplier> supplierList;
	private Supplier supplier;
	private ProductBLO productBLO;
	private List<Product> productList;
	private List<Product> selectedProductList;
	private Product product;
	private ImportBillDetailBLO importBillDetailBLO;
	private List<ImportBillDetail> importBillDetailList;
	private List<Integer> quantityList;
	private List<ImportBillDetail> detailList;
	private ImportBillDetail importBillDetail;

	private int quantity;
	private Long id;

	public ImportBillAction(){
		importBillDetailBLO = new ImportBillDetailBLO();
		importBillBLO=new ImportBillBLO();
		supplierBLO = new SupplierBLO();
		productBLO = new ProductBLO();
	}

	public String execute(){
		detailList = importBillDetailBLO.list();
		importBillList = importBillBLO.list();
		supplierList = supplierBLO.list();
		productList = productBLO.list();
		return SUCCESS;
	}

	public String add(){
		try{
			Long total= 0L;
			ImportBill importBill = new ImportBill();
			importBill.setDateImported(new Date());
			importBill.setSupplier(supplierBLO.getById(supplier.getId()));
			
			Set<ImportBillDetail> importBillDetailSet = new HashSet<ImportBillDetail>();
			for (int i=0;i<importBillDetailList.size();i++){
				ImportBillDetail ibd = importBillDetailList.get(i);
				Product product = productBLO.getById(selectedProductList.get(i).getId());
				
				ibd.setProduct(product);
				ibd.setProductQuantity(quantityList.get(i));
				ibd.setImportBill(importBill);
				ibd.setTotal(BigDecimal.valueOf(Long.valueOf(String.valueOf(ibd.getProductPrice())) * ibd.getProductQuantity()));
				total += Long.valueOf(String.valueOf(ibd.getTotal()));
				importBillDetailSet.add(ibd);
				
				/* Update number of products */
				product.setQuantity(product.getQuantity() + ibd.getProductQuantity());
				productBLO.edit(product);
			}		
			importBill.setTotal(BigDecimal.valueOf(total));
			importBill.setImportBillDetail(importBillDetailSet);
			importBillBLO.add(importBill);
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return SUCCESS;
	}

	public List<ImportBill> getImportBillList() {
		return importBillList;
	}

	public void setImportBillList(List<ImportBill> importBillList) {
		this.importBillList = importBillList;
	}

	public ImportBill getImportBill() {
		return importBill;
	}

	public void setImportBill(ImportBill importBill) {
		this.importBill = importBill;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public List<Supplier> getSupplierList() {
		return supplierList;
	}

	public void setSupplierList(List<Supplier> supplierList) {
		this.supplierList = supplierList;
	}

	public Supplier getSupplier() {
		return supplier;
	}

	public void setSupplier(Supplier supplier) {
		this.supplier = supplier;
	}

	public List<Product> getProductList() {
		return productList;
	}

	public void setProductList(List<Product> productList) {
		this.productList = productList;
	}

	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	public int getQuantity() {
		return quantity;
	}

	public void setQuantity(int quantity) {
		this.quantity = quantity;
	}

	public List<ImportBillDetail> getImportBillDetailList() {
		return importBillDetailList;
	}

	public void setImportBillDetailList(List<ImportBillDetail> importBillDetailList) {
		this.importBillDetailList = importBillDetailList;
	}

	public ImportBillDetail getImportBillDetail() {
		return importBillDetail;
	}

	public void setImportBillDetail(ImportBillDetail importBillDetail) {
		this.importBillDetail = importBillDetail;
	}

	public List<ImportBillDetail> getDetailList() {
		return detailList;
	}

	public void setDetailList(List<ImportBillDetail> detailList) {
		this.detailList = detailList;
	}

	public List<Product> getSelectedProductList() {
		return selectedProductList;
	}

	public void setSelectedProductList(List<Product> selectedProductList) {
		this.selectedProductList = selectedProductList;
	}

	public List<Integer> getQuantityList() {
		return quantityList;
	}

	public void setQuantityList(List<Integer> quantityList) {
		this.quantityList = quantityList;
	}


}
